﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MachineStatusClient
{
    public partial class DJAddUserControl : UserControl
    {
        BContentToSqlServer bts = new BContentToSqlServer();
        private string machineId;
        public DJAddUserControl()
        {
            InitializeComponent();
            //InitializeAutoCloseTimer();
        }

        private void DJAddUserControl_Load(object sender, EventArgs e)
        {
            BindWorkOrdersToDataGridView(machineId);
        }

        public void SetMachineId(string id)
        {
            machineId = id;
        }
        private void BindWorkOrdersToDataGridView(string machineId)
        {
            try
            {
                // 1. 从配置读取设备编码
                //string machineId = ConfigurationManager.AppSettings["DeviceCode"];
                dataGridView2.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                dataGridView2.ColumnHeadersHeight = 50;
                if (string.IsNullOrEmpty(machineId))
                {
                    MessageBox.Show("设备编码未配置！");
                    return;
                }

                // 2. 调用接口获取数据
                List<WorkNoList1> workOrders = bts.GetWorkNo1(machineId);

                // 3. 绑定到DataGridView
                if (workOrders != null && workOrders.Count > 0)
                {
                    // 配置列映射（字段名 -> 列名）
                    //dataGridView1.AutoGenerateColumns = false; // 禁止自动生成列
                    // 设置开始时间和结束时间列的格式
                    if (dataGridView2.Columns.Contains("Column1"))
                    {
                        dataGridView2.Columns["Column1"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                        dataGridView2.Columns["Column1"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    }
                    //if (dataGridView2.Columns.Contains("Column6"))
                    //{
                    //    dataGridView2.Columns["Column6"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                    //    dataGridView2.Columns["Column6"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                    //}
                    // 绑定数据源
                    dataGridView2.DataSource = workOrders;

                    dataGridView2.RowTemplate.Height = 50;
                    //Message.Text = "";
                }
                else
                {
                    //MessageBox.Show("未查询到工单数据");
                    //Message.Text = "未查询到工单排程数据";
                    //Message.ForeColor = Color.Red;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show($"加载数据失败: {ex.Message}");
                //Message.Text = $"加载数据失败: {ex.Message}";
                //Message.ForeColor = Color.Red;
            }
        }

        

        private void buttonBaoCun_Click(object sender, EventArgs e)
        {

        }

        private void buttonQuXiao_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
